package com.lanbu.system.service.impl;


import com.lanbu.common.core.domain.entity.SysDept;
import com.lanbu.common.utils.DateUtils;
import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.common.utils.spring.SpringUtils;
import com.lanbu.common.utils.uuid.IdUtils;

import com.lanbu.system.domain.camera.CaCamera;
import com.lanbu.system.mapper.CaCameraMapper;
import com.lanbu.system.mapper.CaWarnMapper;
import com.lanbu.system.mapper.SysDeptMapper;
import com.lanbu.system.service.ICaCameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 摄像头Service业务层处理
 *
 * @author lanbu
 * @date 2024-03-12
 */
@Service
public class CaCameraServiceImpl implements ICaCameraService {

    @Autowired
    private CaCameraMapper caCameraMapper;
//    @Autowired
//    private EmotionAnalysisResultsMapper emotionAnalysisResultsMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private CaWarnMapper caWarnMapper;

    /**
     * 查询摄像头
     *
     * @param id 摄像头主键
     * @return 摄像头
     */
    @Override
    public CaCamera selectCaCameraById(String id) {
        return caCameraMapper.selectCaCameraById(id);
    }

    /**
     * 查询摄像头列表
     *
     * @param caCamera 摄像头
     * @return 摄像头
     */
    @Override
    public List<CaCamera> selectCaCameraList(CaCamera caCamera) {
        return caCameraMapper.selectCaCameraList(caCamera);
    }

    /**
     * 新增摄像头
     *
     * @param caCamera 摄像头
     * @return 结果
     */
    @Override
    public int insertCaCamera(CaCamera caCamera) {
        caCamera.setId(IdUtils.randomUUID());
        caCamera.setCreateTime(DateUtils.getNowDate());
        caCamera.setDeptId(SecurityUtils.getDeptId().toString());
        return caCameraMapper.insertCaCamera(caCamera);
    }

    /**
     * 修改摄像头
     *
     * @param caCamera 摄像头
     * @return 结果
     */
    @Override
    public int updateCaCamera(CaCamera caCamera) {
        CaCamera caCamera1 = SpringUtils.getAopProxy(this).selectCaCameraById(caCamera.getId());
        if (!caCamera1.getDeptId().equals(caCamera.getDeptId())){
            caWarnMapper.updateByDeptId(caCamera.getId(), caCamera.getDeptId());
        }
        caCamera.setUpdateTime(DateUtils.getNowDate());
        return caCameraMapper.updateCaCamera(caCamera);
    }

    /**
     * 批量删除摄像头
     *
     * @param ids 需要删除的摄像头主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCaCameraByIds(String[] ids) {
        for (String id : ids) {
            if (!caWarnMapper.warnByCameraId(id).isEmpty()) {
                throw new RuntimeException("摄像头存在报警信息，不能删除");
            }
        }
        int caCameraByIds = caCameraMapper.deleteCaCameraByIds(ids);

//        for (String id : ids) {
//            CaCamera caCamera = caCameraMapper.selectCaCameraById(id);
//            emotionAnalysisResultsMapper.deleteEmotionAnalysisResultsByIP(caCamera.getCameraIp());
//
//
//        }

        return caCameraByIds;
    }

    /**
     * 删除摄像头信息
     *
     * @param id 摄像头主键
     * @return 结果
     */
    @Override
    public int deleteCaCameraById(String id) {
        return caCameraMapper.deleteCaCameraById(id);
    }

    @Override
    public List<CaCamera> selectCaCameraListByDeptId(String id) {
        List<CaCamera> caCameraList = new ArrayList<>();
        List<SysDept> sysDeptList = sysDeptMapper.selectChildrenDeptById(Long.valueOf(id));
        //查询自己区域
        SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(id));

        if (sysDeptList == null || sysDeptList.isEmpty()) {
            return caCameraMapper.selectCaCameraListByDeptId(id);
        } else {
            //将查询的数据添加进去
            sysDeptList.add(sysDept);

            sysDeptList.forEach(SysDept -> {
                List<CaCamera> caCameras = caCameraMapper.selectCaCameraListByDeptId(SysDept.getDeptId().toString());
                caCameraList.addAll(caCameras);
            });
        }
        return caCameraList;
    }

    @Override
    public List<String> selectCidListByCidId(String sid) {
        return caCameraMapper.selectCidListByCidId(sid);
    }

    @Override
    public List<CaCamera> selectCaCameraListPing() {
        return caCameraMapper.selectCaCameraListPing();
    }
}
